package com.start.ad.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.ad.entity.StartAdCategoryEntity;
import com.start.ad.mapper.StartAdCategoryMapper;
import com.start.ad.service.StartAdCategoryService;
import com.start.common.entity.StartBaseEntity;
import com.start.common.exceptions.StartBusException;
import com.start.common.utils.TreeModelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StartAdCategoryServiceImpl extends ServiceImpl<StartAdCategoryMapper, StartAdCategoryEntity> implements StartAdCategoryService {
    /**
     * 添加栏目
     *
     * @param entity
     */
    @Override
    public void addCategory(StartAdCategoryEntity entity) {
        if (StringUtils.isAllBlank(entity.getAdCategoryCode())) {
            throw new StartBusException("编码不能为空");
        }
        LambdaQueryWrapper<StartAdCategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StartAdCategoryEntity::getAdCategoryCode, entity.getAdCategoryCode());
        if (count(queryWrapper) > 0) {
            throw new StartBusException("已存在相同编码的标签");
        }
        String parentCode = getAdCategoryById(entity.getParentId());
        String adCategoryCascadeCode = StringUtils.isAllBlank(parentCode) ? entity.getAdCategoryCode() : parentCode + ":" + entity.getAdCategoryCode();
        entity.setAdCategoryCascadeCode(adCategoryCascadeCode);
        save(entity);
    }


    /**
     * 修改栏目
     *
     * @param entity
     */
    @Override
    public void updateCategoryById(StartAdCategoryEntity entity) {
        if (StringUtils.isAllBlank(entity.getAdCategoryCode())) {
            throw new StartBusException("编码不能为空");
        }
        LambdaQueryWrapper<StartAdCategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StartAdCategoryEntity::getAdCategoryCode, entity.getAdCategoryCode());
        queryWrapper.ne(StartBaseEntity::getId, entity.getId());
        if (count(queryWrapper) > 0) {
            throw new StartBusException("已存在相同编码的标签");
        }
        StartAdCategoryEntity oldEntity = getById(entity.getId());
        if (oldEntity.getAdCategoryCode().equals(entity.getAdCategoryCode()) && oldEntity.getParentId().equals(entity.getParentId())) {
            updateById(entity);
            return;
        }
        String oldCode = oldEntity.getAdCategoryCascadeCode();
        String parentCode = getAdCategoryById(entity.getParentId());
        String newCodeSb = StringUtils.isAllBlank(parentCode) ? entity.getAdCategoryCode() : parentCode + ":" + entity.getAdCategoryCode();
        entity.setAdCategoryCascadeCode(newCodeSb);
        getBaseMapper().updateChildCode(oldCode, newCodeSb);
        updateById(entity);

    }

    /**
     * 删除栏目
     *
     * @param id
     */
    @Override
    public void deleteCategoryId(String id) {
        StartAdCategoryEntity commonTag = getById(id);
        LambdaQueryWrapper<StartAdCategoryEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.likeRight(StartAdCategoryEntity::getAdCategoryCascadeCode, commonTag.getAdCategoryCascadeCode());
        remove(lambdaQueryWrapper);
        removeById(id);
    }

    @Override
    public List<StartAdCategoryEntity> getCategoryLisByCode(String code, boolean isTree) {
        if (StringUtils.isAllBlank(code)) {
            LambdaQueryWrapper<StartAdCategoryEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(StartAdCategoryEntity::isAdCategoryIsEnable, true);
            lambdaQueryWrapper.orderByAsc(StartAdCategoryEntity::getAdCategorySortNum);
            List<StartAdCategoryEntity> list = list(lambdaQueryWrapper);
            if (!isTree) {
                return list;
            }
            return TreeModelUtils.createTree(list);
        }
        List<String> codes;

        if (code.contains(",")) {
            codes = Arrays.asList(code.split(","));
        } else {
            codes = new ArrayList<>();
            codes.add(code);
        }
        List<StartAdCategoryEntity> list = new ArrayList<>();
        List<StartAdCategoryEntity> rootList = new ArrayList<>();

        for (String c : codes) {
            StartAdCategoryEntity tagByTagCode = getAdCategoryByCode(c);
            if (null == tagByTagCode) {
                continue;
            }
            rootList.add(tagByTagCode);
            LambdaQueryWrapper<StartAdCategoryEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(StartAdCategoryEntity::isAdCategoryIsEnable, true);
            lambdaQueryWrapper.likeRight(StartAdCategoryEntity::getAdCategoryCascadeCode, c + ":");
            lambdaQueryWrapper.orderByAsc(StartAdCategoryEntity::getAdCategorySortNum);
            list.addAll(list(lambdaQueryWrapper));
        }
        if (!isTree) {
            return list;
        }
        if (rootList.size() > 1) {
            rootList = rootList.stream().sorted(Comparator.comparingInt(StartAdCategoryEntity::getAdCategorySortNum)).collect(Collectors.toList());
            list.addAll(0, rootList);
        }
        return TreeModelUtils.createTree(list);
    }


    /**
     * 根据编码查询广告类型
     *
     * @param code
     * @return
     */
    @Override
    public StartAdCategoryEntity getAdCategoryByCode(String code) {
        LambdaQueryWrapper<StartAdCategoryEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StartAdCategoryEntity::getAdCategoryCode, code);
        return getOne(lambdaQueryWrapper);
    }

    private String getAdCategoryById(String id) {
        StartAdCategoryEntity entity = getById(id);
        if (null == entity) {
            return "";
        }
        if (StringUtils.isNotBlank(entity.getAdCategoryCascadeCode())) {
            return entity.getAdCategoryCascadeCode();
        }
        return entity.getAdCategoryCode();
    }
}
